


var ajax = require("../ajax/ajax");
var mongo = require("../mongo/mongo");

var validator = require("validator");
var moment = require("moment");
var py = require("../comm/py");



function get(req, res, r, c) {

    var para = req.body.para;


    switch (para) {
        case "saveContract":  //获取用户信息

            saveContract(req, res).then(r).catch(c);
            break;
        case "getContract":   //获取用户信息
            getContract(req, res).then(r).catch(c);
            break;
        case "getContractList":   //获取用户信息列表
            getContractList(req, res).then(r).catch(c);
            break;
        default:
    }



}
async function saveContract(req, res) {

    var j = req.body.j;

    if (!validator.isLength(j.name, { min: 1, max: 100 })) {

        throw "名称长度必须在1到100之间"
    }
    j.namePy = py.firstLetter(j.name);

    if (!j._id || !j.createTime) {
        j.createTime = moment().utcOffset(-8)._d;
    }
    try {
        j.createTime = moment(j.createTime)._d;
    } catch (e) {

    } finally {

    }


    var data = await mongo.SaveAsync({

        data: j,
        db: "uzor",
        collection: "contract"


    });



    return data
}
async function getContract(req, res) {
    var j = req.body.j;

    if (!j._id) {
        throw '不能没有id'
    }

    var data = await mongo.findOneAsync({
        data: {
            _id: j._id

        },
        db: "uzor",
        collection: "contract"


    })

    return { info: data }
}


async function getContractList(req) {
    var j = req.body.j



    var data = await mongo.findAsync({

        data: j.data,
        limit: 10,
        db: "uzor",
        collection: "contract",
        sort: { createTime: -1 },
        page: j.page


    })
    return data
}



module.exports = get;